package com.example.mode.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.example.mode.R;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 情绪记录Fragment
 */
public class FocusFragment extends Fragment {
    private RadioGroup rgEmotions;
    private Button btnSave;
    private Button btnDeleteAll;
    private EditText etEmotionDescription;
    private ListView lvHistoryRecords;
    private Map<Integer, String> emotionMap = new HashMap<>();
    // 存储历史记录
    private List<String> recordHistory = new ArrayList<>();
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm", java.util.Locale.CHINA);
    private ArrayAdapter<String> adapter;

    public FocusFragment() {
        // 必需的空构造函数
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // 加载情绪记录布局
        View view = inflater.inflate(R.layout.fragment_focus, container, false);

        // 初始化视图组件
        rgEmotions = view.findViewById(R.id.rg_emotions);
        btnSave = view.findViewById(R.id.btn_save);
        btnDeleteAll = view.findViewById(R.id.btn_delete_all);
        etEmotionDescription = view.findViewById(R.id.et_emotion_description);
        lvHistoryRecords = view.findViewById(R.id.lv_history_records);

        // 初始化情绪映射
        initEmotionMap();

        // 初始化列表适配器
        adapter = new ArrayAdapter<>(requireContext(), android.R.layout.simple_list_item_1, recordHistory);
        lvHistoryRecords.setAdapter(adapter);

        // 保存按钮点击事件
        btnSave.setOnClickListener(v -> saveEmotionRecord());

        // 删除所有记录按钮点击事件
        btnDeleteAll.setOnClickListener(v -> deleteAllRecords());

        return view;
    }

    // 初始化情绪类型映射
    private void initEmotionMap() {
        emotionMap.put(R.id.rb_happy, "😀 快乐");
        emotionMap.put(R.id.rb_sad, "😢 悲伤");
        emotionMap.put(R.id.rb_angry, "😡 愤怒");
        emotionMap.put(R.id.rb_anxious, "😟 焦虑");
        emotionMap.put(R.id.rb_relaxed, "😌 放松");
    }

    // 保存情绪记录
    private void saveEmotionRecord() {
        // 获取选择的情绪类型
        String emotionType = getSelectedEmotionType();
        if (emotionType == null) {
            Toast.makeText(requireContext(), "请选择情绪类型", Toast.LENGTH_SHORT).show();
            return;
        }

        // 获取情绪描述
        String description = etEmotionDescription.getText().toString().trim();

        // 获取当前时间
        String currentTime = dateFormat.format(new Date());
        // 拼接记录（实际项目中应存入数据库）
        String record = "【" + currentTime + "】" + emotionType;
        if (!description.isEmpty()) {
            record += " - " + description;
        }
        record += "\n";
        recordHistory.add(record);

        // 更新列表
        adapter.notifyDataSetChanged();

        // 显示保存成功提示
        Toast.makeText(requireContext(), "情绪记录已保存", Toast.LENGTH_SHORT).show();

        // 重置选择和输入框
        rgEmotions.clearCheck();
        etEmotionDescription.setText("");
    }

    // 获取选择的情绪类型
    private String getSelectedEmotionType() {
        int checkedId = rgEmotions.getCheckedRadioButtonId();
        return emotionMap.get(checkedId);
    }

    // 删除所有记录
    private void deleteAllRecords() {
        recordHistory.clear();
        adapter.notifyDataSetChanged();
        Toast.makeText(requireContext(), "所有记录已删除", Toast.LENGTH_SHORT).show();
    }
}